package com.timothy.android.answer;

import java.util.ArrayList;
import java.util.List;

import com.timothy.android.BaseAnswerActivity;
import com.timothy.android.util.ValueCopyUtil;
import com.timothy.chart.BaseChart;
import com.timothy.chart.CanvasSetting;
import com.timothy.chart.Pie3D;
import com.timothy.service.BackLightService;

import android.R.integer;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.os.Message;
import android.os.RemoteException;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;

public class Pie3DAnswerActivity extends BaseAnswerActivity{
	public final static String LOG_TAG = "Pie3DAnswerActivity";
	public final static int thickness = 15;
	List<Pie3D> pieList = new ArrayList<Pie3D>();
	
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.i(LOG_TAG,"onCreate");
        this.setTitle("Pie3DAnswer");
		
		
		for(int i=0;i<chartList.size();i++){
			Log.i(LOG_TAG,"Loop chartList for PieList");
			
			BaseChart baseChart = chartList.get(i);
			Log.i(LOG_TAG,baseChart.toString());
			
			Pie3D pie3D = new Pie3D();
			try {
				ValueCopyUtil.valueCopy(baseChart, pie3D);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			Log.i(LOG_TAG,pie3D.toString());
			
			if(screenSize == 1){
				pie3D.setOvalStartX(50);
				pie3D.setOvalStartY(100);
				pie3D.setOvalHeight(200);
				pie3D.setOvalWidth(300);
				pie3D.setChoiceTextOrdinateX(100);
				pie3D.setChoiceTextOrdinateY(50);
				pie3D.setAnswerTextOrdinateX(400);
				pie3D.setAnswerTextOrdinateY(400);					
			}
			if(screenSize == 2){
				pie3D.setOvalStartX(50);
				pie3D.setOvalStartY(100);
				pie3D.setOvalHeight(200);
				pie3D.setOvalWidth(300);
				pie3D.setChoiceTextOrdinateX(100);
				pie3D.setChoiceTextOrdinateY(50);
				pie3D.setAnswerTextOrdinateX(400);
				pie3D.setAnswerTextOrdinateY(400);					
			}
			if(screenSize == 3){
				pie3D.setOvalStartX(50);
				pie3D.setOvalStartY(100);
				pie3D.setOvalHeight(200);
				pie3D.setOvalWidth(300);
				pie3D.setChoiceTextOrdinateX(100);
				pie3D.setChoiceTextOrdinateY(50);
				pie3D.setAnswerTextOrdinateX(400);
				pie3D.setAnswerTextOrdinateY(400);					
			}

			pieList.add(pie3D);
		}
		
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		VerticalPie3DView view = new VerticalPie3DView(this);
		setContentView(view);
    }
    
	class VerticalPie3DView extends SurfaceView implements SurfaceHolder.Callback{
		private SurfaceHolder mHolder;
		private Canvas canvas;
		
		@Override
		public boolean onTouchEvent(MotionEvent event) {
			recoverBrightness();
			return super.onTouchEvent(event);
		}
		
		public VerticalPie3DView(Context context) {
			super(context);
			mHolder = getHolder();
			mHolder.addCallback(this);
		}

		@Override
		public void surfaceCreated(SurfaceHolder holder) {
			canvas = mHolder.lockCanvas();
			
			int canvasHeight = canvas.getHeight();
			int canvasWidth = canvas.getWidth();
			Log.i(LOG_TAG, "VerticalPie3DView:Canvas Height:"+canvasHeight);
			Log.i(LOG_TAG, "VerticalPie3DView:Canvas Width:"+canvasWidth);
			
			CanvasSetting setting = new CanvasSetting(canvasHeight,canvasWidth,Color.BLACK,1);
			
			int leftM = setting.getLeftMargin();
			int rightM = setting.getRightMargin();
			int topM = setting.getTopMargin();
			int bottomM = setting.getBottomMargin();
			
			Log.i(LOG_TAG, "VerticalPie2DView:leftM:"+leftM);
			Log.i(LOG_TAG, "VerticalPie2DView:rightM:"+rightM);
			Log.i(LOG_TAG, "VerticalPie2DView:topM:"+topM);
			Log.i(LOG_TAG, "VerticalPie2DView:bottomM:"+bottomM);
			
			canvas.drawColor(setting.getBgColor());// 设置画布背景为黑色
			
			//write question
//			Paint mPaint = new Paint();
//			mPaint.setColor(Color.parseColor(questionTextColor));		
//			mPaint.setTextSize(textSize);
//			int queX = setting.getLeftMargin();
//			int queY = setting.getTopMargin();
//			Log.i(LOG_TAG, "VerticalPie3DView:queX:"+queX);
//			Log.i(LOG_TAG, "VerticalPie3DView:queY:"+queY);
//			canvas.drawText(answer.getQuestion().getQuestionName(),queX,queY/2,mPaint); 
//			canvas.save();
			
			//write question
			Paint mPaint = new Paint();
			mPaint.setColor(Color.parseColor(questionTextColor));		
			mPaint.setTextSize(textSize);
			int queX = setting.getLeftMargin();
			int queY = setting.getTopMargin();
			Log.i(LOG_TAG, "VerticalBar2DView:queX:"+queX);
			Log.i(LOG_TAG, "VerticalBar2DView:queY:"+queY);
			String questionString = baseBean.getQuestionText();
			if("42".equals(type)&&priority!=null){
				questionString = questionString + "(priority = "+priority+")";
			}
			Log.i(LOG_TAG, "VerticalBar2DView:questionString:"+questionString);
			int queLength = questionString.length();
			Log.i(LOG_TAG, "VerticalBar2DView:queLength:"+queLength);
			int line = 0;
			int lineHeight = 0;
			if(queLength>QUESTION_WRAP_LENGTH_VERTIAL){
				Log.i(LOG_TAG, "VerticalBar2DView:draw question text->queLength>"+QUESTION_WRAP_LENGTH_VERTIAL);
				for(line = 0;line<queLength/QUESTION_WRAP_LENGTH_VERTIAL;line++){
					canvas.drawText(questionString.substring(QUESTION_WRAP_LENGTH_VERTIAL*line,QUESTION_WRAP_LENGTH_VERTIAL*(line+1)),queX,queY/4+lineHeight,mPaint);
					Log.i(LOG_TAG, "VerticalBar2DView:start:"+QUESTION_WRAP_LENGTH_VERTIAL*line+";end:"+QUESTION_WRAP_LENGTH_VERTIAL*(line+1));
					lineHeight+= queY/4;
				}
				Log.i(LOG_TAG, "VerticalBar2DView:start:"+QUESTION_WRAP_LENGTH_VERTIAL*line);
				canvas.drawText(questionString.substring(line*QUESTION_WRAP_LENGTH_VERTIAL),queX,queY/4+lineHeight,mPaint);
			}else{
				Log.i(LOG_TAG, "VerticalBar2DView:draw question text->queLength<="+QUESTION_WRAP_LENGTH_VERTIAL);
				canvas.drawText(questionString,queX,queY/2,mPaint);
			}
			canvas.save();
			
			int pieWidth = canvasWidth - (int)(setting.getLeftMargin()*1.1 + setting.getRightMargin()*1.1);
			int pieHeight = pieWidth/2;

			int startX = (int)(setting.getLeftMargin()*1.1);
			int startY = (int)(setting.getTopMargin()*1.1);
			int endX = startX + pieWidth;
			int endY = startY + pieHeight;
			
			for(int j=0;j<=thickness;j++){
				float startAngle = 0;
				for(int i=0;i<pieList.size();i++){
					Pie3D pie3D = pieList.get(i);
					mPaint.setColor(Color.parseColor(pie3D.getShadowColor()));
					
//					RectF recF = new RectF(pie3D.getOvalStartX(),pie3D.getOvalStartY()-j,
//							pie3D.getOvalStartX()+pie3D.getOvalWidth(),pie3D.getOvalHeight()-j);
					
					RectF recF = new RectF(startX,startY-j,endX,endY-j);
					float sweepAngle = new Float(pie3D.getShare()*360) ;
					canvas.drawArc(recF, startAngle, sweepAngle, true, mPaint);
					startAngle = startAngle + sweepAngle;
					canvas.save();
				}
				
				if(j==thickness){
					startAngle = 0;
					for(int i=0;i<pieList.size();i++){
						Pie3D pie3D = pieList.get(i);
						mPaint.setColor(Color.parseColor(pie3D.getFullColor()));
						
//						RectF recF = new RectF(pie3D.getOvalStartX(),pie3D.getOvalStartY()-thickness,
//								pie3D.getOvalStartX()+pie3D.getOvalWidth(),pie3D.getOvalHeight()-thickness);
						
						RectF recF = new RectF(startX,startY-thickness,endX,endY-thickness);
						
						float sweepAngle = new Float(pie3D.getShare()*360) ;
						canvas.drawArc(recF, startAngle, sweepAngle, true, mPaint);
						startAngle = startAngle + sweepAngle;
						canvas.save();
						
						//draw choice text
//						mPaint.setTextSize(pie3D.getChoiceTextSize());
//						mPaint.setColor(Color.parseColor(pie3D.getFullColor()));
//						canvas.drawText(pie3D.getChoiceText()+"("+pie3D.getAnswerText()+")", startX, startY+pieHeight+100+i*50, mPaint);
//						canvas.save();
						
						int textStartY = (canvasHeight/10)* 4; 
						int textEndY = canvasHeight - setting.getBottomMargin();
						int textHeight = (textEndY - textStartY)/10;
						
						int textX = startX; 
						int textY = textStartY+(i+1)*textHeight;
						
						Log.i(LOG_TAG, "textStartY:" + textStartY);
						Log.i(LOG_TAG, "textEndY:" + textEndY);
						Log.i(LOG_TAG, "textHeight:" + textHeight);
						
						Log.i(LOG_TAG, "textX:" + textX);
						Log.i(LOG_TAG, "textY:" + textY);
						
						//draw choice text
						mPaint.setTextSize(pie3D.getChoiceTextSize());
						mPaint.setColor(Color.parseColor(pie3D.getFullColor()));
						canvas.drawText(pie3D.getChoiceText()+"("+pie3D.getAnswerText()+")", textX,textY, mPaint);
						canvas.save();
					}
				}
				

			}

			//draw border
			mPaint.setColor(Color.WHITE);
			for(int x=1;x<10;x++){
				//draw horizontal lines
				canvas.drawLine(leftM,x* canvasHeight/10, canvasWidth-rightM,x*canvasHeight/10, mPaint);
			}
			for(int y=1;y<10;y++){
				//draw vertical lines
				canvas.drawLine(y* canvasWidth/10,topM, y* canvasWidth/10, canvasHeight-bottomM, mPaint);
			}
			
			canvas.restore();
			mHolder.unlockCanvasAndPost(canvas);
			
		}
	

		@Override
		public void surfaceChanged(SurfaceHolder holder, int format, int width,
				int height) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void surfaceDestroyed(SurfaceHolder holder) {
			// TODO Auto-generated method stub
		}
		
	}
    
    
}
